﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MultiWindows.Codes;
using System.Configuration;

namespace MultiWindows.Forms
{
    public partial class FormMain : Form
    {
        public static string VideoURL { get; set; }
        public static string VideoPath { get; set; }
        public static string VideoDVD { get; set; }
        public static string Announcement { get; set; }
        private IniFiles ini { get; set; }

        public FormMain()
        {
            InitializeComponent();
        }

        private void menuNews_Click(object sender, EventArgs e)
        {
            if (!CheckWindowExist(Window.NEWS))
            {
                FormNews frmNews = new FormNews();
                frmNews.MdiParent = this;
                ShowWindow(frmNews);
            }
        }

        private void menuTest_Click(object sender, EventArgs e)
        {
            
        }

        private void menuDateTime_Click(object sender, EventArgs e)
        {
            if (!CheckWindowExist(Window.DATETIME))
            {
                FormDateTime frmDateTime = new FormDateTime();
                frmDateTime.MdiParent = this;
                frmDateTime.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                ShowWindow(frmDateTime);
            }
        }

        private void menuVideo_Click(object sender, EventArgs e)
        {
            if (!CheckWindowExist(Window.VIDEO))
            {
                FormVideo frmVideo = new FormVideo();
                frmVideo.MdiParent = this;
                if (VideoURL != "")
                {
                    frmVideo.url = VideoURL;
                }
                else if (VideoPath != "")
                {
                    frmVideo.url = VideoPath;
                }
                
                //frmVideo.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                ShowWindow(frmVideo);
                
            }
        }

        private void menuAnnouncement_Click(object sender, EventArgs e)
        {
            if (!CheckWindowExist(Window.ANNOUNCEMENT))
            {
                FormAnnouncement frmAnnouncement = new FormAnnouncement();
                frmAnnouncement.MdiParent = this;
                frmAnnouncement.path = Announcement;
                ShowWindow(frmAnnouncement);
            }
        }

        private void menuAll_Click(object sender, EventArgs e)
        {
            menuAnnouncement_Click(sender, e);
            menuDateTime_Click(sender, e);
            menuNews_Click(sender, e);
            menuVideo_Click(sender, e);
        }

        private void menuSetAnnouncement_Click(object sender, EventArgs e)
        {
            this.openFileDialog.FileName = "";
            this.openFileDialog.Filter = "TXT file|*.txt";
            if (this.openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Announcement = this.openFileDialog.FileName;
                SaveSettings();
                RefreshOpenedAnnouncement();
            }
        }

        private void menuSetVideoURL_Click(object sender, EventArgs e)
        {
            
        }

        private void menuSetVideoURL_Local_Click(object sender, EventArgs e)
        {
            this.openFileDialog.Filter = "所有视频文件(*.avi;*.mkv;*.rmvb;*.mpeg;*.mp4)|*.avi;*.mkv;*.rmvb;*.mpeg;*.mp4";
            this.openFileDialog.FileName = "";
            if (this.openFileDialog.ShowDialog() == DialogResult.OK)
            {
                VideoPath = this.openFileDialog.FileName;
                VideoURL = "";
                VideoDVD = "";
                SaveSettings();
                RefreshOpenedVideo();
            }
        }

        private void menuSetVideoURL_DVD_Click(object sender, EventArgs e)
        {

        }

        private void menuSetVideoURL_Network_Click(object sender, EventArgs e)
        {
            FormVideoSetURL frmVideoSetURL = new FormVideoSetURL();
            frmVideoSetURL.MdiParent = this;
            frmVideoSetURL.Show();
        }

        private void LoadSettings()
        {
            ini = new IniFiles(ConfigurationManager.AppSettings["configuration"].ToString());
            //ini.WriteString("Video", "URL", "http://edu.sse.com.cn/sseportal/newedu/pages/p1441/content/1.swf?fid=lsmx_1_1");
            //ini.WriteString("Announcement", "PATH", "c:\\1.txt");
            VideoURL = ini.ReadString(Window.VIDEO, "URL", "");
            VideoPath = ini.ReadString(Window.VIDEO, "Path", "");
            VideoDVD = ini.ReadString(Window.VIDEO, "DVD", "");
            Announcement = ini.ReadString(Window.ANNOUNCEMENT, "PATH", "");
        }

        public void SaveSettings()
        {
            ini.WriteString(Window.VIDEO, "URL", VideoURL);
            ini.WriteString(Window.VIDEO, "Path", VideoPath);
            ini.WriteString(Window.VIDEO, "DVD", VideoDVD);
            ini.WriteString(Window.ANNOUNCEMENT, "PATH", Announcement);
        }

        public void SaveWindowState(string window, int height, int width, int top, int left)
        {
            ini.WriteInteger(window, "Height", height);
            ini.WriteInteger(window, "Width", width);
            ini.WriteInteger(window, "Top", top);
            ini.WriteInteger(window, "Left", left);
        }


        public void RefreshOpenedVideo()
        {
            foreach (Form frm in this.MdiChildren)
            {
                if (frm.Name == "Form" + Window.VIDEO)
                {
                    if (VideoURL != "")
                    {
                        ((FormVideo)frm).url = VideoURL;
                    }
                    else if (VideoPath != "")
                    {
                        ((FormVideo)frm).url = VideoPath;
                    }
                    ((FormVideo)frm).Play();
                }
            }
        }

        private void RefreshOpenedAnnouncement()
        {
            foreach (Form frm in this.MdiChildren)
            {
                if (frm.Name == "Form" + Window.ANNOUNCEMENT)
                {
                    ((FormAnnouncement)frm).path = Announcement;
                    ((FormAnnouncement)frm).LoadAnnouncement();
                }
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            LoadSettings();
        }

        private bool CheckWindowExist(string name)
        {
            foreach (Form frm in this.MdiChildren)
            {
                if (frm.Name == "Form" + name)
                {
                    return true;
                }
            }

            return false;
        }

        private void ShowWindow(Form form)
        {
            string name = form.Name.Substring(4);
            int height = this.ini.ReadInteger(name, "Height", 100);
            int width = this.ini.ReadInteger(name, "Width", 100);
            int top = this.ini.ReadInteger(name, "Top", 100);
            int left = this.ini.ReadInteger(name, "Left", 100);

            form.Show();
            form.Height = height;
            form.Width = width;
            form.Top = top;
            form.Left = left;
        }

        

        
       
    }
}
